
#include <inttypes.h>
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "BSP.h"
#include "ModbusReg.h"
#include "agile_modbus.h"

#include <stdio.h>
#include <string.h>




/*	MODBUS--保持寄存器相关定义	*/
#define DEVICE_INFO_MODBUS_ADDRESS 			(0x0000UL)
static AppDeviceInfos device_info_reg;

#define APP_SETTINGS_MODBUS_ADDRESS 		(0x0100UL)
static AppSettings app_set_reg;

#define APP_STATUS_MODBUS_ADDRESS 			(0x0400UL)
static AppStatus app_status_reg;

#define APP_INPUT_MODBUS_ADDRESS 			(0x0600UL)
static AppInputs app_inputs_reg;

#define APP_OUTPUT_MODBUS_ADDRESS 			(0x0800UL)
static AppOutputs app_outputs_reg;

#define APP_OVERRIDES_MODBUS_ADDRESS 		(0x0900UL)
static AppOverrides  app_overrides_reg;

#define APP_ALARMS_MODBUS_ADDRESS 			(0x0A00UL)
static AppAlarms	app_alarms_reg;

#define DEBUG_MODBUS_ADDRESS 				(0xE000UL)
static DebudRWs debug_rw_reg;

#define CALIBRATION_MODBUS_ADDRESS 			(0xF000UL)
static AppCalibrations app_calibrations_reg;

#define AI_CALI_MODBUS_ADDRESS 			    (0xF800UL)
static AppAnlogInputCalibrations  app_ai_cali_reg;

#define AO_CALI_MODBUS_ADDRESS 			    (0xF900UL)
static AppAnlogOutputCalibrations  app_ao_cali_reg;


/*	MODBUS--线圈寄存器相关定义	*/
#define REG_COIL_START                 	( 1200U )
#define REG_COIL_NREGS               		( 16U )
static volatile uint16_t usRegCoilStart = REG_COIL_START;
static uint8_t usRegCoilBuf[REG_COIL_NREGS] = { 0X01, 0X01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00 };


/*	MODBUS--离散输入寄存器相关定义	*/
#define REG_DISC_START                 	( 1300U )
#define REG_DISC_NREGS               		( 16U )
static volatile uint16_t usRegDiscStart = REG_DISC_START;
static uint8_t  usRegDiscBuf[REG_DISC_NREGS] = { 0X01, 0X01, 0x00, 0x00, 0x01, 0x01, 0x00, 0x00 };


/*	MODBUS--输入寄存器相关定义	*/
static DebudROs debug_ro_reg;


static uint8_t modbus_buf[2048];




/*	系统参数	*/
extern SystemParameter SysParam;
/*	系统状态	*/
extern SystemState SysState;


extern EventGroupHandle_t SysEvent;				        //系统事件  





/**
	* @brief  app_set_reg结构体数据填充
  * @param  None
  * @retval None
  */
static void app_set_reg_fill( void ) 
{
	memset( &app_set_reg, 0, sizeof(AppSettings) );

	if( SysParam.ModbusRtuSlaveParam.Baudrate == 9600 ) 
		app_set_reg.ModbusRtuSlaveParam.Baudrate = 0;
	else if( SysParam.ModbusRtuSlaveParam.Baudrate == 19200 ) 
		app_set_reg.ModbusRtuSlaveParam.Baudrate = 1;
	else if( SysParam.ModbusRtuSlaveParam.Baudrate == 38400 ) 
		app_set_reg.ModbusRtuSlaveParam.Baudrate = 2;
	else if( SysParam.ModbusRtuSlaveParam.Baudrate == 57600 ) 
		app_set_reg.ModbusRtuSlaveParam.Baudrate = 3;
	else if( SysParam.ModbusRtuSlaveParam.Baudrate == 115200 ) 
		app_set_reg.ModbusRtuSlaveParam.Baudrate = 4;

	app_set_reg.ModbusRtuSlaveParam.Parity = SysParam.ModbusRtuSlaveParam.Parity;
	app_set_reg.ModbusRtuSlaveParam.StopBits = SysParam.ModbusRtuSlaveParam.StopBits;
	app_set_reg.ModbusRtuSlaveParam.TxdDelay = SysParam.ModbusRtuSlaveParam.TxdDelay;
	app_set_reg.ModbusRtuSlaveParam.T35Time = SysParam.ModbusRtuSlaveParam.T35Time;
	
	app_set_reg.WorkParam.WorkMode = SysParam.WorkParam.WorkMode;
	app_set_reg.WorkParam.NetId = SysParam.WorkParam.NetId;
	app_set_reg.WorkParam.NetChannel = SysParam.WorkParam.NetChannel;
	app_set_reg.WorkParam.DeviceType = SysParam.WorkParam.DeviceType;
	app_set_reg.WorkParam.SensorType = SysParam.WorkParam.SensorType;

	app_set_reg.AppParam.ResetCount = SysParam.AppParam.ResetCount;

	app_set_reg.AppParam.AtpsSaTempCali = SysParam.AppParam.AtpsSaTempCali;
	app_set_reg.AppParam.AtpsSaRhCali = SysParam.AppParam.AtpsSaRhCali;

	app_set_reg.AppParam.AtpsOaTempCali = SysParam.AppParam.AtpsOaTempCali;
	app_set_reg.AppParam.AtpsOaRhCali = SysParam.AppParam.AtpsOaRhCali;

	app_set_reg.AppParam.AtpsRaTempCali = SysParam.AppParam.AtpsRaTempCali;
	app_set_reg.AppParam.AtpsRaRhCali = SysParam.AppParam.AtpsRaRhCali;

	app_set_reg.AppParam.AtpsMaTempCali = SysParam.AppParam.AtpsMaTempCali;
	app_set_reg.AppParam.AtpsMaRhCali = SysParam.AppParam.AtpsMaRhCali;

	app_set_reg.AppParam.AtpsExaTempCali = SysParam.AppParam.AtpsExaTempCali;
	app_set_reg.AppParam.AtpsExaRhCali = SysParam.AppParam.AtpsExaRhCali;

	app_set_reg.AppParam.AtpsRoomaTempCali = SysParam.AppParam.AtpsRoomaTempCali;
	app_set_reg.AppParam.AtpsRoomaRhCali = SysParam.AppParam.AtpsRoomaRhCali;

	app_set_reg.AppParam.AtpsCoolaTempCali = SysParam.AppParam.AtpsCoolaTempCali;
	app_set_reg.AppParam.AtpsCoolaRhCali = SysParam.AppParam.AtpsCoolaRhCali;

	app_set_reg.AppParam.AtpsHeataTempCali = SysParam.AppParam.AtpsHeataTempCali;
	app_set_reg.AppParam.AtpsHeataRhCali = SysParam.AppParam.AtpsHeataRhCali;

	app_set_reg.AppParam.AtpsHumaTempCali = SysParam.AppParam.AtpsHumaTempCali;
	app_set_reg.AppParam.AtpsHumaRhCali = SysParam.AppParam.AtpsHumaRhCali;

	app_set_reg.AppParam.AtpsPreTempCali = SysParam.AppParam.AtpsPreTempCali;
	app_set_reg.AppParam.AtpsPreRhCali = SysParam.AppParam.AtpsPreRhCali;

	app_set_reg.AppParam.AtpsRotTempCali = SysParam.AppParam.AtpsRotTempCali;
	app_set_reg.AppParam.AtpsRotRhCali = SysParam.AppParam.AtpsRotRhCali;

	app_set_reg.AppParam.Atmosphere = SysParam.AppParam.Atmosphere;

	app_set_reg.AppParam.SamplingPeriod = SysParam.AppParam.SamplingPeriod;
	app_set_reg.AppParam.AverageWindowSize = SysParam.AppParam.AverageWindowSize;
}



/**
	* @brief  app_alarms_reg结构体数据填充
  * @param  None
  * @retval None
  */
static void app_alarms_reg_fill( void )
{
	memset( &app_alarms_reg, 0, sizeof( AppAlarms ) );
	
	app_alarms_reg.Reserved = 0;
}



/**
	* @brief  app_calibrations_reg结构体数据填充
  * @param  None
  * @retval None
  */
static void app_calibrations_reg_fill( void )
{
	memset( &app_calibrations_reg, 0, sizeof( AppCalibrations ) );
	
	app_calibrations_reg.Reserved = 0;
}



/**
	* @brief  app_inputs_reg结构体数据填充
  * @param  None
  * @retval None
  */
static void app_inputs_reg_fill( void )
{
	memset( &app_inputs_reg, 0, sizeof( AppInputs ) );

	app_inputs_reg.AtpsSaParam.TDB = SysState.AtpsSaParam.TDB;
	app_inputs_reg.AtpsSaParam.TDP = SysState.AtpsSaParam.TDP;
	app_inputs_reg.AtpsSaParam.TWB = SysState.AtpsSaParam.TWB;
	app_inputs_reg.AtpsSaParam.HR = SysState.AtpsSaParam.HR;
	app_inputs_reg.AtpsSaParam.RH = SysState.AtpsSaParam.RH;
	app_inputs_reg.AtpsSaParam.H = SysState.AtpsSaParam.H;

	app_inputs_reg.AtpsSaParam.SensorFault = SysState.AtpsSaParam.SensorFault;
	app_inputs_reg.AtpsSaParam.SensorFaultCnt = SysState.AtpsSaParam.SensorFaultCnt;

	app_inputs_reg.AtpsSaParam.CommFault = SysState.AtpsSaParam.CommFault;
	app_inputs_reg.AtpsSaParam.CommFaultCnt = SysState.AtpsSaParam.CommFaultCnt;

	if(  SysState.AtpsSaParam.Rssi >= -40 ) app_inputs_reg.AtpsSaParam.Rssi = 5;
	else if(  SysState.AtpsSaParam.Rssi >= -60 && SysState.AtpsSaParam.Rssi < -40 ) app_inputs_reg.AtpsSaParam.Rssi = 4;
	else if(  SysState.AtpsSaParam.Rssi >= -70 && SysState.AtpsSaParam.Rssi < -60 ) app_inputs_reg.AtpsSaParam.Rssi = 3;
	else if(  SysState.AtpsSaParam.Rssi >= -80 && SysState.AtpsSaParam.Rssi < -70 ) app_inputs_reg.AtpsSaParam.Rssi = 2;
	else if(  SysState.AtpsSaParam.Rssi >= -100 && SysState.AtpsSaParam.Rssi < -80 ) app_inputs_reg.AtpsSaParam.Rssi = 1;
	else app_inputs_reg.AtpsSaParam.Rssi = 0;

	app_inputs_reg.AtpsOaParam.TDB = SysState.AtpsOaParam.TDB;
	app_inputs_reg.AtpsOaParam.TDP = SysState.AtpsOaParam.TDP;
	app_inputs_reg.AtpsOaParam.TWB = SysState.AtpsOaParam.TWB;
	app_inputs_reg.AtpsOaParam.HR = SysState.AtpsOaParam.HR;
	app_inputs_reg.AtpsOaParam.RH = SysState.AtpsOaParam.RH;
	app_inputs_reg.AtpsOaParam.H = SysState.AtpsOaParam.H;

	app_inputs_reg.AtpsOaParam.SensorFault = SysState.AtpsOaParam.SensorFault;
	app_inputs_reg.AtpsOaParam.SensorFaultCnt = SysState.AtpsOaParam.SensorFaultCnt;

	app_inputs_reg.AtpsOaParam.CommFault = SysState.AtpsOaParam.CommFault;
	app_inputs_reg.AtpsOaParam.CommFaultCnt = SysState.AtpsOaParam.CommFaultCnt;

	if(  SysState.AtpsOaParam.Rssi >= -40 ) app_inputs_reg.AtpsOaParam.Rssi = 5;
	else if(  SysState.AtpsOaParam.Rssi >= -60 && SysState.AtpsOaParam.Rssi < -40 ) app_inputs_reg.AtpsOaParam.Rssi = 4;
	else if(  SysState.AtpsOaParam.Rssi >= -70 && SysState.AtpsOaParam.Rssi < -60 ) app_inputs_reg.AtpsOaParam.Rssi = 3;
	else if(  SysState.AtpsOaParam.Rssi >= -80 && SysState.AtpsOaParam.Rssi < -70 ) app_inputs_reg.AtpsOaParam.Rssi = 2;
	else if(  SysState.AtpsOaParam.Rssi >= -100 && SysState.AtpsOaParam.Rssi < -80 ) app_inputs_reg.AtpsOaParam.Rssi = 1;
	else app_inputs_reg.AtpsOaParam.Rssi = 0;


	app_inputs_reg.AtpsRaParam.TDB = SysState.AtpsRaParam.TDB;
	app_inputs_reg.AtpsRaParam.TDP = SysState.AtpsRaParam.TDP;
	app_inputs_reg.AtpsRaParam.TWB = SysState.AtpsRaParam.TWB;
	app_inputs_reg.AtpsRaParam.HR = SysState.AtpsRaParam.HR;
	app_inputs_reg.AtpsRaParam.RH = SysState.AtpsRaParam.RH;
	app_inputs_reg.AtpsRaParam.H = SysState.AtpsRaParam.H;

	app_inputs_reg.AtpsRaParam.SensorFault = SysState.AtpsRaParam.SensorFault;
	app_inputs_reg.AtpsRaParam.SensorFaultCnt = SysState.AtpsRaParam.SensorFaultCnt;

	app_inputs_reg.AtpsRaParam.CommFault = SysState.AtpsRaParam.CommFault;
	app_inputs_reg.AtpsRaParam.CommFaultCnt = SysState.AtpsRaParam.CommFaultCnt;

	if(  SysState.AtpsRaParam.Rssi >= -40 ) app_inputs_reg.AtpsRaParam.Rssi = 5;
	else if(  SysState.AtpsRaParam.Rssi >= -60 && SysState.AtpsRaParam.Rssi < -40 ) app_inputs_reg.AtpsRaParam.Rssi = 4;
	else if(  SysState.AtpsRaParam.Rssi >= -70 && SysState.AtpsRaParam.Rssi < -60 ) app_inputs_reg.AtpsRaParam.Rssi = 3;
	else if(  SysState.AtpsRaParam.Rssi >= -80 && SysState.AtpsRaParam.Rssi < -70 ) app_inputs_reg.AtpsRaParam.Rssi = 2;
	else if(  SysState.AtpsRaParam.Rssi >= -100 && SysState.AtpsRaParam.Rssi < -80 ) app_inputs_reg.AtpsRaParam.Rssi = 1;
	else app_inputs_reg.AtpsRaParam.Rssi = 0;


	app_inputs_reg.AtpsMaParam.TDB = SysState.AtpsMaParam.TDB;
	app_inputs_reg.AtpsMaParam.TDP = SysState.AtpsMaParam.TDP;
	app_inputs_reg.AtpsMaParam.TWB = SysState.AtpsMaParam.TWB;
	app_inputs_reg.AtpsMaParam.HR = SysState.AtpsMaParam.HR;
	app_inputs_reg.AtpsMaParam.RH = SysState.AtpsMaParam.RH;
	app_inputs_reg.AtpsMaParam.H = SysState.AtpsMaParam.H;

	app_inputs_reg.AtpsMaParam.SensorFault = SysState.AtpsMaParam.SensorFault;
	app_inputs_reg.AtpsMaParam.SensorFaultCnt = SysState.AtpsMaParam.SensorFaultCnt;

	app_inputs_reg.AtpsMaParam.CommFault = SysState.AtpsMaParam.CommFault;
	app_inputs_reg.AtpsMaParam.CommFaultCnt = SysState.AtpsMaParam.CommFaultCnt;

	if(  SysState.AtpsMaParam.Rssi >= -40 ) app_inputs_reg.AtpsMaParam.Rssi = 5;
	else if(  SysState.AtpsMaParam.Rssi >= -60 && SysState.AtpsMaParam.Rssi < -40 ) app_inputs_reg.AtpsMaParam.Rssi = 4;
	else if(  SysState.AtpsMaParam.Rssi >= -70 && SysState.AtpsMaParam.Rssi < -60 ) app_inputs_reg.AtpsMaParam.Rssi = 3;
	else if(  SysState.AtpsMaParam.Rssi >= -80 && SysState.AtpsMaParam.Rssi < -70 ) app_inputs_reg.AtpsMaParam.Rssi = 2;
	else if(  SysState.AtpsMaParam.Rssi >= -100 && SysState.AtpsMaParam.Rssi < -80 ) app_inputs_reg.AtpsMaParam.Rssi = 1;
	else app_inputs_reg.AtpsMaParam.Rssi = 0;


	app_inputs_reg.AtpsExaParam.TDB = SysState.AtpsExaParam.TDB;
	app_inputs_reg.AtpsExaParam.TDP = SysState.AtpsExaParam.TDP;
	app_inputs_reg.AtpsExaParam.TWB = SysState.AtpsExaParam.TWB;
	app_inputs_reg.AtpsExaParam.HR = SysState.AtpsExaParam.HR;
	app_inputs_reg.AtpsExaParam.RH = SysState.AtpsExaParam.RH;
	app_inputs_reg.AtpsExaParam.H = SysState.AtpsExaParam.H;

	app_inputs_reg.AtpsExaParam.SensorFault = SysState.AtpsExaParam.SensorFault;
	app_inputs_reg.AtpsExaParam.SensorFaultCnt = SysState.AtpsExaParam.SensorFaultCnt;

	app_inputs_reg.AtpsExaParam.CommFault = SysState.AtpsExaParam.CommFault;
	app_inputs_reg.AtpsExaParam.CommFaultCnt = SysState.AtpsExaParam.CommFaultCnt;

	if(  SysState.AtpsExaParam.Rssi >= -40 ) app_inputs_reg.AtpsExaParam.Rssi = 5;
	else if(  SysState.AtpsExaParam.Rssi >= -60 && SysState.AtpsExaParam.Rssi < -40 ) app_inputs_reg.AtpsExaParam.Rssi = 4;
	else if(  SysState.AtpsExaParam.Rssi >= -70 && SysState.AtpsExaParam.Rssi < -60 ) app_inputs_reg.AtpsExaParam.Rssi = 3;
	else if(  SysState.AtpsExaParam.Rssi >= -80 && SysState.AtpsExaParam.Rssi < -70 ) app_inputs_reg.AtpsExaParam.Rssi = 2;
	else if(  SysState.AtpsExaParam.Rssi >= -100 && SysState.AtpsExaParam.Rssi < -80 ) app_inputs_reg.AtpsExaParam.Rssi = 1;
	else app_inputs_reg.AtpsExaParam.Rssi = 0;


	app_inputs_reg.AtpsRoomaParam.TDB = SysState.AtpsRoomaParam.TDB;
	app_inputs_reg.AtpsRoomaParam.TDP = SysState.AtpsRoomaParam.TDP;
	app_inputs_reg.AtpsRoomaParam.TWB = SysState.AtpsRoomaParam.TWB;
	app_inputs_reg.AtpsRoomaParam.HR = SysState.AtpsRoomaParam.HR;
	app_inputs_reg.AtpsRoomaParam.RH = SysState.AtpsRoomaParam.RH;
	app_inputs_reg.AtpsRoomaParam.H = SysState.AtpsRoomaParam.H;

	app_inputs_reg.AtpsRoomaParam.SensorFault = SysState.AtpsRoomaParam.SensorFault;
	app_inputs_reg.AtpsRoomaParam.SensorFaultCnt = SysState.AtpsRoomaParam.SensorFaultCnt;

	app_inputs_reg.AtpsRoomaParam.CommFault = SysState.AtpsRoomaParam.CommFault;
	app_inputs_reg.AtpsRoomaParam.CommFaultCnt = SysState.AtpsRoomaParam.CommFaultCnt;

	if(  SysState.AtpsRoomaParam.Rssi >= -40 ) app_inputs_reg.AtpsRoomaParam.Rssi = 5;
	else if(  SysState.AtpsRoomaParam.Rssi >= -60 && SysState.AtpsRoomaParam.Rssi < -40 ) app_inputs_reg.AtpsRoomaParam.Rssi = 4;
	else if(  SysState.AtpsRoomaParam.Rssi >= -70 && SysState.AtpsRoomaParam.Rssi < -60 ) app_inputs_reg.AtpsRoomaParam.Rssi = 3;
	else if(  SysState.AtpsRoomaParam.Rssi >= -80 && SysState.AtpsRoomaParam.Rssi < -70 ) app_inputs_reg.AtpsRoomaParam.Rssi = 2;
	else if(  SysState.AtpsRoomaParam.Rssi >= -100 && SysState.AtpsRoomaParam.Rssi < -80 ) app_inputs_reg.AtpsRoomaParam.Rssi = 1;
	else app_inputs_reg.AtpsRoomaParam.Rssi = 0;


	app_inputs_reg.AtpsCoolaParam.TDB = SysState.AtpsCoolaParam.TDB;
	app_inputs_reg.AtpsCoolaParam.TDP = SysState.AtpsCoolaParam.TDP;
	app_inputs_reg.AtpsCoolaParam.TWB = SysState.AtpsCoolaParam.TWB;
	app_inputs_reg.AtpsCoolaParam.HR = SysState.AtpsCoolaParam.HR;
	app_inputs_reg.AtpsCoolaParam.RH = SysState.AtpsCoolaParam.RH;
	app_inputs_reg.AtpsCoolaParam.H = SysState.AtpsCoolaParam.H;

	app_inputs_reg.AtpsCoolaParam.SensorFault = SysState.AtpsCoolaParam.SensorFault;
	app_inputs_reg.AtpsCoolaParam.SensorFaultCnt = SysState.AtpsCoolaParam.SensorFaultCnt;

	app_inputs_reg.AtpsCoolaParam.CommFault = SysState.AtpsCoolaParam.CommFault;
	app_inputs_reg.AtpsCoolaParam.CommFaultCnt = SysState.AtpsCoolaParam.CommFaultCnt;

	if(  SysState.AtpsCoolaParam.Rssi >= -40 ) app_inputs_reg.AtpsCoolaParam.Rssi = 5;
	else if(  SysState.AtpsCoolaParam.Rssi >= -60 && SysState.AtpsCoolaParam.Rssi < -40 ) app_inputs_reg.AtpsCoolaParam.Rssi = 4;
	else if(  SysState.AtpsCoolaParam.Rssi >= -70 && SysState.AtpsCoolaParam.Rssi < -60 ) app_inputs_reg.AtpsCoolaParam.Rssi = 3;
	else if(  SysState.AtpsCoolaParam.Rssi >= -80 && SysState.AtpsCoolaParam.Rssi < -70 ) app_inputs_reg.AtpsCoolaParam.Rssi = 2;
	else if(  SysState.AtpsCoolaParam.Rssi >= -100 && SysState.AtpsCoolaParam.Rssi < -80 ) app_inputs_reg.AtpsCoolaParam.Rssi = 1;
	else app_inputs_reg.AtpsCoolaParam.Rssi = 0;


	app_inputs_reg.AtpsHeataParam.TDB = SysState.AtpsHeataParam.TDB;
	app_inputs_reg.AtpsHeataParam.TDP = SysState.AtpsHeataParam.TDP;
	app_inputs_reg.AtpsHeataParam.TWB = SysState.AtpsHeataParam.TWB;
	app_inputs_reg.AtpsHeataParam.HR = SysState.AtpsHeataParam.HR;
	app_inputs_reg.AtpsHeataParam.RH = SysState.AtpsHeataParam.RH;
	app_inputs_reg.AtpsHeataParam.H = SysState.AtpsHeataParam.H;

	app_inputs_reg.AtpsHeataParam.SensorFault = SysState.AtpsHeataParam.SensorFault;
	app_inputs_reg.AtpsHeataParam.SensorFaultCnt = SysState.AtpsHeataParam.SensorFaultCnt;

	app_inputs_reg.AtpsHeataParam.CommFault = SysState.AtpsHeataParam.CommFault;
	app_inputs_reg.AtpsHeataParam.CommFaultCnt = SysState.AtpsHeataParam.CommFaultCnt;

	if(  SysState.AtpsHeataParam.Rssi >= -40 ) app_inputs_reg.AtpsHeataParam.Rssi = 5;
	else if(  SysState.AtpsHeataParam.Rssi >= -60 && SysState.AtpsHeataParam.Rssi < -40 ) app_inputs_reg.AtpsHeataParam.Rssi = 4;
	else if(  SysState.AtpsHeataParam.Rssi >= -70 && SysState.AtpsHeataParam.Rssi < -60 ) app_inputs_reg.AtpsHeataParam.Rssi = 3;
	else if(  SysState.AtpsHeataParam.Rssi >= -80 && SysState.AtpsHeataParam.Rssi < -70 ) app_inputs_reg.AtpsHeataParam.Rssi = 2;
	else if(  SysState.AtpsHeataParam.Rssi >= -100 && SysState.AtpsHeataParam.Rssi < -80 ) app_inputs_reg.AtpsHeataParam.Rssi = 1;
	else app_inputs_reg.AtpsHeataParam.Rssi = 0;


	app_inputs_reg.AtpsHumaParam.TDB = SysState.AtpsHumaParam.TDB;
	app_inputs_reg.AtpsHumaParam.TDP = SysState.AtpsHumaParam.TDP;
	app_inputs_reg.AtpsHumaParam.TWB = SysState.AtpsHumaParam.TWB;
	app_inputs_reg.AtpsHumaParam.HR = SysState.AtpsHumaParam.HR;
	app_inputs_reg.AtpsHumaParam.RH = SysState.AtpsHumaParam.RH;
	app_inputs_reg.AtpsHumaParam.H = SysState.AtpsHumaParam.H;

	app_inputs_reg.AtpsHumaParam.SensorFault = SysState.AtpsHumaParam.SensorFault;
	app_inputs_reg.AtpsHumaParam.SensorFaultCnt = SysState.AtpsHumaParam.SensorFaultCnt;

	app_inputs_reg.AtpsHumaParam.CommFault = SysState.AtpsHumaParam.CommFault;
	app_inputs_reg.AtpsHumaParam.CommFaultCnt = SysState.AtpsHumaParam.CommFaultCnt;

	if(  SysState.AtpsHumaParam.Rssi >= -40 ) app_inputs_reg.AtpsHumaParam.Rssi = 5;
	else if(  SysState.AtpsHumaParam.Rssi >= -60 && SysState.AtpsHumaParam.Rssi < -40 ) app_inputs_reg.AtpsHumaParam.Rssi = 4;
	else if(  SysState.AtpsHumaParam.Rssi >= -70 && SysState.AtpsHumaParam.Rssi < -60 ) app_inputs_reg.AtpsHumaParam.Rssi = 3;
	else if(  SysState.AtpsHumaParam.Rssi >= -80 && SysState.AtpsHumaParam.Rssi < -70 ) app_inputs_reg.AtpsHumaParam.Rssi = 2;
	else if(  SysState.AtpsHumaParam.Rssi >= -100 && SysState.AtpsHumaParam.Rssi < -80 ) app_inputs_reg.AtpsHumaParam.Rssi = 1;
	else app_inputs_reg.AtpsHumaParam.Rssi = 0;


	app_inputs_reg.AtpsPreParam.TDB = SysState.AtpsPreParam.TDB;
	app_inputs_reg.AtpsPreParam.TDP = SysState.AtpsPreParam.TDP;
	app_inputs_reg.AtpsPreParam.TWB = SysState.AtpsPreParam.TWB;
	app_inputs_reg.AtpsPreParam.HR = SysState.AtpsPreParam.HR;
	app_inputs_reg.AtpsPreParam.RH = SysState.AtpsPreParam.RH;
	app_inputs_reg.AtpsPreParam.H = SysState.AtpsPreParam.H;

	app_inputs_reg.AtpsPreParam.SensorFault = SysState.AtpsPreParam.SensorFault;
	app_inputs_reg.AtpsPreParam.SensorFaultCnt = SysState.AtpsPreParam.SensorFaultCnt;

	app_inputs_reg.AtpsPreParam.CommFault = SysState.AtpsPreParam.CommFault;
	app_inputs_reg.AtpsPreParam.CommFaultCnt = SysState.AtpsPreParam.CommFaultCnt;

	if(  SysState.AtpsPreParam.Rssi >= -40 ) app_inputs_reg.AtpsPreParam.Rssi = 5;
	else if(  SysState.AtpsPreParam.Rssi >= -60 && SysState.AtpsPreParam.Rssi < -40 ) app_inputs_reg.AtpsPreParam.Rssi = 4;
	else if(  SysState.AtpsPreParam.Rssi >= -70 && SysState.AtpsPreParam.Rssi < -60 ) app_inputs_reg.AtpsPreParam.Rssi = 3;
	else if(  SysState.AtpsPreParam.Rssi >= -80 && SysState.AtpsPreParam.Rssi < -70 ) app_inputs_reg.AtpsPreParam.Rssi = 2;
	else if(  SysState.AtpsPreParam.Rssi >= -100 && SysState.AtpsPreParam.Rssi < -80 ) app_inputs_reg.AtpsPreParam.Rssi = 1;
	else app_inputs_reg.AtpsPreParam.Rssi = 0;


	app_inputs_reg.AtpsRotParam.TDB = SysState.AtpsRotParam.TDB;
	app_inputs_reg.AtpsRotParam.TDP = SysState.AtpsRotParam.TDP;
	app_inputs_reg.AtpsRotParam.TWB = SysState.AtpsRotParam.TWB;
	app_inputs_reg.AtpsRotParam.HR = SysState.AtpsRotParam.HR;
	app_inputs_reg.AtpsRotParam.RH = SysState.AtpsRotParam.RH;
	app_inputs_reg.AtpsRotParam.H = SysState.AtpsRotParam.H;

	app_inputs_reg.AtpsRotParam.SensorFault = SysState.AtpsRotParam.SensorFault;
	app_inputs_reg.AtpsRotParam.SensorFaultCnt = SysState.AtpsRotParam.SensorFaultCnt;

	app_inputs_reg.AtpsRotParam.CommFault = SysState.AtpsRotParam.CommFault;
	app_inputs_reg.AtpsRotParam.CommFaultCnt = SysState.AtpsRotParam.CommFaultCnt;

	if(  SysState.AtpsRotParam.Rssi >= -40 ) app_inputs_reg.AtpsRotParam.Rssi = 5;
	else if(  SysState.AtpsRotParam.Rssi >= -60 && SysState.AtpsRotParam.Rssi < -40 ) app_inputs_reg.AtpsRotParam.Rssi = 4;
	else if(  SysState.AtpsRotParam.Rssi >= -70 && SysState.AtpsRotParam.Rssi < -60 ) app_inputs_reg.AtpsRotParam.Rssi = 3;
	else if(  SysState.AtpsRotParam.Rssi >= -80 && SysState.AtpsRotParam.Rssi < -70 ) app_inputs_reg.AtpsRotParam.Rssi = 2;
	else if(  SysState.AtpsRotParam.Rssi >= -100 && SysState.AtpsRotParam.Rssi < -80 ) app_inputs_reg.AtpsRotParam.Rssi = 1;
	else app_inputs_reg.AtpsRotParam.Rssi = 0;
	
}



/**
	* @brief  app_outputs_reg结构体数据填充
  * @param  None
  * @retval None
  */
static void app_outputs_reg_fill( void )
{
	memset( &app_outputs_reg, 0, sizeof( AppOutputs ) );
	
  	app_outputs_reg.Reserved = 0;
	
}



/**
	* @brief  app_overrides_reg结构体数据填充
  * @param  None
  * @retval None
  */
static void app_overrides_reg_fill( void )
{
	memset( &app_overrides_reg, 0, sizeof( AppOverrides ) );
	
  	app_overrides_reg.Reserved = 0;
}


/**
	* @brief  device_info_reg结构体数据填充
  * @param  None
  * @retval None
  */
static void device_info_reg_fill( void )
{
	device_info_reg.MANUFACTURER_ID = MANUFACTURER_ID_DEFAULT_VALUE;
	device_info_reg.MODEL_NUMBER = MODEL_NUMBER_DEFAULT_VALUE;
	device_info_reg.HARDWARE_REVISION_NUMBER = HARDWARE_REVISION_NUMBER_DEFAULT_VALUE;
	device_info_reg.FIRMWARE_REVISION_NUMBER = FIRMWARE_REVISION_NUMBER_DEFAULT_VALUE;
	device_info_reg.PCBA_MANUFACTURING_DATE_CODE = PCBA_MANUFACTURING_DATE_CODE_VALUE;
	device_info_reg.PCBA_SERIAL_NUMBER = PCBA_SERIAL_NUMBER_VALUE;
	
	device_info_reg.MODBUS_BAUD_RATE = 0;
	if( SysParam.ModbusRtuSlaveParam.Baudrate == 2400 ) device_info_reg.MODBUS_BAUD_RATE |= 0x100;
	else if( SysParam.ModbusRtuSlaveParam.Baudrate == 4800 ) device_info_reg.MODBUS_BAUD_RATE |= 0x200;
	else if( SysParam.ModbusRtuSlaveParam.Baudrate == 9600 ) device_info_reg.MODBUS_BAUD_RATE |= 0x400;
	else if( SysParam.ModbusRtuSlaveParam.Baudrate == 19200 ) device_info_reg.MODBUS_BAUD_RATE |= 0x800;
	else if( SysParam.ModbusRtuSlaveParam.Baudrate == 38400 ) device_info_reg.MODBUS_BAUD_RATE |= 0x1000;
	else if( SysParam.ModbusRtuSlaveParam.Baudrate == 115200 ) device_info_reg.MODBUS_BAUD_RATE |= 0x2000;
	
	if( SysParam.ModbusRtuSlaveParam.Parity == 0 && SysParam.ModbusRtuSlaveParam.StopBits == 2 )  device_info_reg.MODBUS_BAUD_RATE |= 0x01;
	else if( SysParam.ModbusRtuSlaveParam.Parity == 1 && SysParam.ModbusRtuSlaveParam.StopBits == 1 )  device_info_reg.MODBUS_BAUD_RATE |= 0x03;
	else if( SysParam.ModbusRtuSlaveParam.Parity == 2 && SysParam.ModbusRtuSlaveParam.StopBits == 1 )  device_info_reg.MODBUS_BAUD_RATE |= 0x02;
	
	device_info_reg.MODBUS_ADDRESS = SysParam.WorkParam.DeviceType+20;
	
	device_info_reg.MODBUS_DELAY_REPLY = SysParam.ModbusRtuSlaveParam.TxdDelay;
	device_info_reg.MODBUS_T35 = SysParam.ModbusRtuSlaveParam.T35Time;
	device_info_reg.MODBUS_RESET = 0;
}



/**
	* @brief  app_ai_cali_reg结构体数据填充
  * @param  None
  * @retval None
  */
static void app_ai_cali_reg_fill( void )
{
  app_ai_cali_reg.Reserved = 0;
}



/**
	* @brief  app_ao_cali_reg结构体数据填充
  * @param  None
  * @retval None
  */
static void app_ao_cali_reg_fill( void )
{
  app_ao_cali_reg.Reserved = 0;
}



/**
	* @brief  初始化Modbus相关寄存器
  * @param  None
  * @retval None
  */
static void ModbusReg_Init( void )
{
	memset( &device_info_reg, 0, sizeof( AppDeviceInfos ) );
//	printf("AppDeviceInfos size: %d\r\n", sizeof( AppDeviceInfos ) );
//	printf("AppDeviceInfos reg: %d\r\n", sizeof( AppDeviceInfos ) / 2 );
	
	memset( &app_inputs_reg, 0, sizeof( AppInputs ) );
//	printf("AppInputs size: %d\r\n", sizeof( AppInputs ) );
//	printf("AppInputs reg: %d\r\n", sizeof( AppInputs ) / 2 );
	
  memset( &app_outputs_reg, 0, sizeof( AppOutputs ) );
//	printf("AppOutputs size: %d\r\n", sizeof( AppOutputs ) );
//	printf("AppOutputs reg: %d\r\n", sizeof( AppOutputs ) / 2 );
	
	memset( &app_overrides_reg, 0, sizeof( AppOverrides ) );
//	printf("AppOverrides size: %d\r\n", sizeof( AppOverrides ) );
//	printf("AppOverrides reg: %d\r\n", sizeof( AppOverrides ) / 2 );
	
	memset( &debug_rw_reg, 0, sizeof( DebudRWs ) );
//	printf("DebudRWs size: %d\r\n", sizeof(  DebudRWs ) );
//	printf("DebudRWs reg: %d\r\n", sizeof( DebudRWs ) / 2 );
	
	memset( &debug_ro_reg, 0, sizeof( DebudROs ) );
//	printf("DebudROs size: %d\r\n", sizeof(  DebudROs ) );
//	printf("DebudROs reg: %d\r\n", sizeof( DebudROs ) / 2 );
	
	memset( &app_calibrations_reg, 0, sizeof( AppCalibrations ) );
//	printf("AppCalibrations size: %d\r\n", sizeof( AppCalibrations ) );
//	printf("AppCalibrations reg: %d\r\n", sizeof( AppCalibrations ) / 2 );
	
	memset( &app_alarms_reg, 0, sizeof( AppAlarms ) );
//	printf("AppAlarms size: %d\r\n", sizeof( AppAlarms ) );
//	printf("AppAlarms reg: %d\r\n", sizeof( AppAlarms ) / 2 );
	
	memset( &app_status_reg, 0, sizeof( AppStatus ) );
//	printf("AppStatus size: %d\r\n", sizeof( AppStatus ) );
//	printf("AppStatus reg: %d\r\n", sizeof( AppStatus ) / 2 );
	
	memset( &app_set_reg, 0, sizeof( AppSettings ) );
//	printf("AppSettings size: %d\r\n", sizeof( AppSettings ) );
//	printf("AppSettings reg: %d\r\n", sizeof( AppSettings ) / 2 );
}



/**
	* @brief DeviceInfo寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void  DeviceInfoReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint16_t *dat = (uint16_t *)modbus_buf;
	
	iRegIndex = ( usAddress - DEVICE_INFO_MODBUS_ADDRESS );
	
	device_info_reg_fill( );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&device_info_reg, sizeof(AppDeviceInfos) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}



/**
	* @brief DeviceInfo寄存器--写操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void DeviceInfoReg_Write( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t temp;
	AppDeviceInfos reg;
  	uint32_t flags_to_set = 0;
	uint16_t *dat = (uint16_t *)modbus_buf; 
	
	iRegIndex = ( usAddress - DEVICE_INFO_MODBUS_ADDRESS );
	
	device_info_reg_fill( );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&device_info_reg, sizeof(AppDeviceInfos) );

	while( usNRegs > 0 )
	{
		dat[iRegIndex] = *RegBuffer++ << 8;
		dat[iRegIndex] |= *RegBuffer++;
		iRegIndex++;
		usNRegs--;
	}	
	
	memcpy( (unsigned char *)&reg, modbus_buf, sizeof(AppDeviceInfos) );
	
	// if( reg.MODBUS_ADDRESS != device_info_reg.MODBUS_ADDRESS )
	// {
	// 	if( reg.MODBUS_ADDRESS >= 20 && reg.MODBUS_ADDRESS <= 30 )
	// 	{
	// 		SysParam.COM1ModbusParam.SlaveAddr = reg.MODBUS_ADDRESS;
	// 	}
		
	// 	flags_to_set |= Com1ParamSaveFlag;
	// }
	
	if( reg.MODBUS_BAUD_RATE != device_info_reg.MODBUS_BAUD_RATE )
	{
		temp = reg.MODBUS_BAUD_RATE >> 8;
		if( temp == 0x01 )SysParam.ModbusRtuSlaveParam.Baudrate = 2400;
		else if( temp == 0x02 )SysParam.ModbusRtuSlaveParam.Baudrate = 4800;
		else if( temp == 0x04 )SysParam.ModbusRtuSlaveParam.Baudrate = 9600;
		else if( temp == 0x08 )SysParam.ModbusRtuSlaveParam.Baudrate = 19200;
		else if( temp == 0x10 )SysParam.ModbusRtuSlaveParam.Baudrate = 38400;
		else if( temp == 0x20 )SysParam.ModbusRtuSlaveParam.Baudrate = 115200;
		
		temp = reg.MODBUS_BAUD_RATE & 0x00ff;
		if( temp == 0 )
		{
			SysParam.ModbusRtuSlaveParam.Parity = 0;
			SysParam.ModbusRtuSlaveParam.StopBits = 1;
		}
		else if( temp == 0x01 )
		{
			SysParam.ModbusRtuSlaveParam.Parity = 0;
			SysParam.ModbusRtuSlaveParam.StopBits = 2;
		}
		else if( temp == 0x02 )
		{
			SysParam.ModbusRtuSlaveParam.Parity = 2;
			SysParam.ModbusRtuSlaveParam.StopBits = 1;
		}
		else if( temp == 0x03 )
		{
			SysParam.ModbusRtuSlaveParam.Parity = 1;
			SysParam.ModbusRtuSlaveParam.StopBits = 1;
		}
		
		flags_to_set |= ModbusRtuSlaveParamSaveFlag;
	}
	
	if( reg.MODBUS_DELAY_REPLY != device_info_reg.MODBUS_DELAY_REPLY && \
			reg.MODBUS_DELAY_REPLY >= 1 && \
			reg.MODBUS_DELAY_REPLY <= 1000 )
	{
		SysParam.ModbusRtuSlaveParam.TxdDelay = reg.MODBUS_DELAY_REPLY;			//MODBUS串口数据延时发送时间，单位：ms
		flags_to_set |= ModbusRtuSlaveParamSaveFlag;
	}
	
	if( reg.MODBUS_T35 != device_info_reg.MODBUS_T35 && \
			reg.MODBUS_T35 >= 0 && \
			reg.MODBUS_T35 <= 20 )
	{
		SysParam.ModbusRtuSlaveParam.T35Time = reg.MODBUS_T35;	//MODBUS数据帧间隔时间，超过这个值，则判断为一帧数据接收完成，单位：ms
		flags_to_set |= ModbusRtuSlaveParamSaveFlag;
	}
	
	if( reg.MODBUS_RESET == 0x55aa )
	{
		flags_to_set |= SysResetFlag;
	}
	else if( reg.MODBUS_RESET == 0xaaaa )
	{
    flags_to_set |= FactoryResetFlag;
		flags_to_set |= SysResetFlag;
	}
	else if( reg.MODBUS_RESET == 0xbbaa )
	{
    flags_to_set |= FactoryResetFlag;
		flags_to_set |= SysResetFlag;
	}
	
	memcpy( (uint8_t *)&device_info_reg, (uint8_t *)&reg, sizeof(AppDeviceInfos) );

	xEventGroupSetBits(SysEvent,  flags_to_set);
}




/**
	* @brief AppInput寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AppInputReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t i;
	uint16_t *dat = (uint16_t *)modbus_buf;
	
	app_inputs_reg_fill( );
	
	
	iRegIndex = ( usAddress - APP_INPUT_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_inputs_reg, sizeof(AppInputs) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}


/**
	* @brief AppOutput寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AppOutputReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t i;
	uint16_t *dat = (uint16_t *)modbus_buf;
  
  	app_outputs_reg_fill( );
	
	iRegIndex = ( usAddress - APP_OUTPUT_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_outputs_reg, sizeof(AppOutputs) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}




/**
	* @brief AppOutputReg寄存器--写操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AppOutputReg_Write( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t i;
	AppOutputs reg;
	uint16_t *dat = (uint16_t *)modbus_buf; 
  
  	app_outputs_reg_fill( );
	
	iRegIndex = ( usAddress - APP_OUTPUT_MODBUS_ADDRESS );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_outputs_reg, sizeof(AppOutputs) );

	while( usNRegs > 0 )
	{
		dat[iRegIndex] = *RegBuffer++ << 8;
		dat[iRegIndex] |= *RegBuffer++;
		iRegIndex++;
		usNRegs--;
	}	
	
	memcpy( (uint8_t *)&reg, modbus_buf, sizeof(AppOutputs) );
}




/**
	* @brief AppOverridesReg寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AppOverridesReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
//	uint8_t i;
	uint16_t *dat = (uint16_t *)modbus_buf;
  
  	app_overrides_reg_fill( );
	
	iRegIndex = ( usAddress - APP_OVERRIDES_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_overrides_reg, sizeof(AppOverrides) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}




/**
	* @brief AppOverridesReg寄存器--写操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AppOverridesReg_Write( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex, i;
	AppOverrides reg;
	uint16_t *dat = (uint16_t *)modbus_buf; 
  
  	app_overrides_reg_fill( );
	
	iRegIndex = ( usAddress - APP_OVERRIDES_MODBUS_ADDRESS );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_overrides_reg, sizeof(AppOverrides) );

	while( usNRegs > 0 )
	{
		dat[iRegIndex] = *RegBuffer++ << 8;
		dat[iRegIndex] |= *RegBuffer++;
		iRegIndex++;
		usNRegs--;
	}	
	
	memcpy( (uint8_t *)&reg, modbus_buf, sizeof(AppOverrides) );
}



/**
	* @brief DebugRWReg寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void DebugRWReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t i;
	uint16_t *dat = (uint16_t *)modbus_buf;
	
  	memset( &debug_rw_reg, 0, sizeof(DebudRWs) );
  
	iRegIndex = ( usAddress - DEBUG_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&debug_rw_reg, sizeof(DebudRWs) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}




/**
	* @brief DebugRWReg寄存器--写操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void DebugRWReg_Write( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t i, flag = 0;
	DebudRWs reg;
	uint16_t *dat = (uint16_t *)modbus_buf; 
	
  	memset( &debug_rw_reg, 0, sizeof(DebudRWs) );
  
	iRegIndex = ( usAddress - DEBUG_MODBUS_ADDRESS );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&debug_rw_reg, sizeof(DebudRWs) );

	while( usNRegs > 0 )
	{
		dat[iRegIndex] = *RegBuffer++ << 8;
		dat[iRegIndex] |= *RegBuffer++;
		iRegIndex++;
		usNRegs--;
	}	
	
	memcpy( (uint8_t *)&debug_rw_reg, modbus_buf, sizeof(DebudRWs) );
	
}



/**
	* @brief DebugROReg寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void DebugROReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t i;
	uint16_t *dat = (uint16_t *)modbus_buf;
	
	memset( &debug_ro_reg, 0, sizeof(DebudROs) );
	
	iRegIndex = ( usAddress - DEBUG_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&debug_ro_reg, sizeof(DebudROs) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}




/**
	* @brief CalibrationsReg寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void CalibrationsReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t i;
	uint16_t *dat = (uint16_t *)modbus_buf;

	app_calibrations_reg_fill( );
	
	
	iRegIndex = ( usAddress - CALIBRATION_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_calibrations_reg, sizeof(AppCalibrations) );
	
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}




/**
	* @brief CalibrationsReg寄存器--写操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void CalibrationsReg_Write( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
//	uint8_t temp, i, flag = 0;
//	int32_t val1, val2;
//	int32_t num;
	AppCalibrations reg;
	uint16_t *dat = (uint16_t *)modbus_buf; 
	
	iRegIndex = ( usAddress - CALIBRATION_MODBUS_ADDRESS );
	
	app_calibrations_reg_fill( );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_calibrations_reg, sizeof(AppCalibrations) );

	while( usNRegs > 0 )
	{
		dat[iRegIndex] = *RegBuffer++ << 8;
		dat[iRegIndex] |= *RegBuffer++;
		iRegIndex++;
		usNRegs--;
	}	
	
	memcpy( (uint8_t *)&reg, modbus_buf, sizeof(AppCalibrations) );
}




/**
	* @brief AppAlarms寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AppAlarmsReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t i;
	uint16_t *dat = (uint16_t *)modbus_buf;
	float val1, val2;

	app_alarms_reg_fill( );
	
	
	iRegIndex = ( usAddress - APP_ALARMS_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_alarms_reg, sizeof(AppAlarms) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}




/**
	* @brief AppAlarms寄存器--写操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AppAlarmsReg_Write( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
//	uint8_t temp, i;
	AppAlarms reg;
	uint16_t *dat = (uint16_t *)modbus_buf; 
	
	iRegIndex = ( usAddress - APP_ALARMS_MODBUS_ADDRESS );
	
	app_alarms_reg_fill( );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_alarms_reg, sizeof(AppAlarms) );

	while( usNRegs > 0 )
	{
		dat[iRegIndex] = *RegBuffer++ << 8;
		dat[iRegIndex] |= *RegBuffer++;
		iRegIndex++;
		usNRegs--;
	}	
	
	memcpy( (uint8_t *)&reg, modbus_buf, sizeof(AppAlarms) );
}




/**
	* @brief AppStatus寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void  AppStatusReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
//	uint8_t i, temp;
	uint16_t *dat = (uint16_t *)modbus_buf;
	
	app_status_reg.NVS_Fault = SysState.NVS_Fault;
	app_status_reg.SysRunTime = SysState.SysRunTime;
	app_status_reg.NetConnectFlag = SysState.NetConnectFlag;
	app_status_reg.ChipTemp = SysState.ChipTemp;
	
  
	iRegIndex = ( usAddress - APP_STATUS_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_status_reg, sizeof(AppStatus) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}




/**
	* @brief AppSet寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void  AppSetReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t i, temp;
	uint16_t *dat = (uint16_t *)modbus_buf;
	
	app_set_reg_fill( );
	
	
	iRegIndex = ( usAddress - APP_SETTINGS_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_set_reg, sizeof(AppSettings) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}




/**
	* @brief AppSet寄存器--写操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AppSetReg_Write( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint8_t temp1, temp2, i = 0;
 	uint32_t flags_to_set = 0;
	AppSettings reg;
	int32_t val1, val2;
	uint16_t *dat = (uint16_t *)modbus_buf; 
	
  
  	app_set_reg_fill( );
  
	iRegIndex = ( usAddress - APP_SETTINGS_MODBUS_ADDRESS );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_set_reg, sizeof(AppSettings) );

	while( usNRegs > 0 )
	{
		dat[iRegIndex] = *RegBuffer++ << 8;
		dat[iRegIndex] |= *RegBuffer++;
		iRegIndex++;
		usNRegs--;
	}	
	
	memcpy( (uint8_t *)&reg, modbus_buf, sizeof(AppSettings) );
	
	
	if( memcmp( (uint8_t *)&reg, (uint8_t *)&app_set_reg, sizeof(AppSettings) ) != 0 )
	{
		if( memcmp( (uint8_t *)&reg.ModbusRtuSlaveParam, (uint8_t *)&app_set_reg.ModbusRtuSlaveParam, sizeof(tModbusRtuSlaveParameter) ) != 0 )
		{
			if( reg.ModbusRtuSlaveParam.Baudrate != app_set_reg.ModbusRtuSlaveParam.Baudrate )
			{
				if( reg.ModbusRtuSlaveParam.Baudrate == 0 ) SysParam.ModbusRtuSlaveParam.Baudrate = 9600;
				else if( reg.ModbusRtuSlaveParam.Baudrate == 1 ) SysParam.ModbusRtuSlaveParam.Baudrate = 19200;
				else if( reg.ModbusRtuSlaveParam.Baudrate == 2 ) SysParam.ModbusRtuSlaveParam.Baudrate = 38400;
				else if( reg.ModbusRtuSlaveParam.Baudrate == 3 ) SysParam.ModbusRtuSlaveParam.Baudrate = 57600;
				else if( reg.ModbusRtuSlaveParam.Baudrate == 4 ) SysParam.ModbusRtuSlaveParam.Baudrate = 115200;
				
				flags_to_set |= ModbusRtuSlaveParamSaveFlag;
			}
			
			if( (reg.ModbusRtuSlaveParam.Parity != app_set_reg.ModbusRtuSlaveParam.Parity) && \
				(reg.ModbusRtuSlaveParam.Parity <= 2) )
			{
				SysParam.ModbusRtuSlaveParam.Parity = reg.ModbusRtuSlaveParam.Parity;
				flags_to_set |= ModbusRtuSlaveParamSaveFlag;
			}
			
			if( (reg.ModbusRtuSlaveParam.StopBits != app_set_reg.ModbusRtuSlaveParam.StopBits) && \
				(reg.ModbusRtuSlaveParam.StopBits <= 2) && \
				(reg.ModbusRtuSlaveParam.StopBits >= 1) )
			{
				SysParam.ModbusRtuSlaveParam.StopBits = reg.ModbusRtuSlaveParam.StopBits;
				flags_to_set |= ModbusRtuSlaveParamSaveFlag;
			}
			
			if( (reg.ModbusRtuSlaveParam.TxdDelay != app_set_reg.ModbusRtuSlaveParam.TxdDelay) && \
				(reg.ModbusRtuSlaveParam.TxdDelay >= 1) && \
				(reg.ModbusRtuSlaveParam.TxdDelay <= 5000) )
			{
				SysParam.ModbusRtuSlaveParam.TxdDelay = reg.ModbusRtuSlaveParam.TxdDelay;			//MODBUS串口数据延时发送时间，单位：ms
				flags_to_set |= ModbusRtuSlaveParamSaveFlag;
			}
			
			if( (reg.ModbusRtuSlaveParam.T35Time != app_set_reg.ModbusRtuSlaveParam.T35Time) && \
				(reg.ModbusRtuSlaveParam.T35Time >= 0) && \
				(reg.ModbusRtuSlaveParam.T35Time <= 40)  )
			{
				SysParam.ModbusRtuSlaveParam.T35Time = reg.ModbusRtuSlaveParam.T35Time;	//MODBUS数据帧间隔时间，超过这个值，则判断为一帧数据接收完成，单位：ms
				flags_to_set |= ModbusRtuSlaveParamSaveFlag;
			}

		}
		
		if( memcmp( (uint8_t *)&reg.WorkParam, (uint8_t *)&app_set_reg.WorkParam, sizeof(tWorkParameter) ) != 0 )
		{
			if( (reg.WorkParam.WorkMode <= WirelessMaster) &&
				(reg.WorkParam.WorkMode != app_set_reg.WorkParam.WorkMode) )
			{
				SysParam.WorkParam.WorkMode = reg.WorkParam.WorkMode;
				flags_to_set |= WorkParamSaveFlag;
			}

			if( (reg.WorkParam.NetId <= 50) &&
				(reg.WorkParam.NetId >= 1) &&
				(reg.WorkParam.NetId != app_set_reg.WorkParam.NetId) )
			{
				SysParam.WorkParam.NetId = reg.WorkParam.NetId;
				flags_to_set |= WorkParamSaveFlag;
			}

			if( (reg.WorkParam.NetChannel <= 100) &&
				(reg.WorkParam.NetChannel >= 1) &&
				(reg.WorkParam.NetChannel != app_set_reg.WorkParam.NetChannel) )
			{
				SysParam.WorkParam.NetChannel = reg.WorkParam.NetChannel;
				flags_to_set |= WorkParamSaveFlag;
			}

			if( (reg.WorkParam.DeviceType <= AtpsRot) &&
				(reg.WorkParam.DeviceType != app_set_reg.WorkParam.DeviceType) )
			{
				SysParam.WorkParam.DeviceType = reg.WorkParam.DeviceType;
				flags_to_set |= WorkParamSaveFlag;
			}

			if( (reg.WorkParam.SensorType <= SHT41) &&
				(reg.WorkParam.SensorType != app_set_reg.WorkParam.SensorType) )
			{
				SysParam.WorkParam.SensorType = reg.WorkParam.SensorType;
				flags_to_set |= WorkParamSaveFlag;
			}
		}


		if( memcmp( (uint8_t *)&reg.AppParam, (uint8_t *)&app_set_reg.AppParam, sizeof(tAppParameter) ) != 0 )
		{
			if( (reg.AppParam.ResetCount != app_set_reg.AppParam.ResetCount) &&
				(reg.AppParam.ResetCount == 0) )
			{
				SysParam.AppParam.ResetCount = 0;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsSaTempCali != app_set_reg.AppParam.AtpsSaTempCali) &&
				(reg.AppParam.AtpsSaTempCali >= -30) && 
				(reg.AppParam.AtpsSaTempCali <= 110) )
			{
				SysParam.AppParam.AtpsSaTempCali = reg.AppParam.AtpsSaTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsSaRhCali != app_set_reg.AppParam.AtpsSaRhCali) &&
				(reg.AppParam.AtpsSaRhCali >= -30) && 
				(reg.AppParam.AtpsSaRhCali <= 110) )
			{
				SysParam.AppParam.AtpsSaRhCali = reg.AppParam.AtpsSaRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsOaTempCali != app_set_reg.AppParam.AtpsOaTempCali) &&
				(reg.AppParam.AtpsOaTempCali >= -30) && 
				(reg.AppParam.AtpsOaTempCali <= 110) )
			{
				SysParam.AppParam.AtpsOaTempCali = reg.AppParam.AtpsOaTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsOaRhCali != app_set_reg.AppParam.AtpsOaRhCali) &&
				(reg.AppParam.AtpsOaRhCali >= -30) && 
				(reg.AppParam.AtpsOaRhCali <= 110) )
			{
				SysParam.AppParam.AtpsOaRhCali = reg.AppParam.AtpsOaRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsRaTempCali != app_set_reg.AppParam.AtpsRaTempCali) &&
				(reg.AppParam.AtpsRaTempCali >= -30) && 
				(reg.AppParam.AtpsRaTempCali <= 110) )
			{
				SysParam.AppParam.AtpsRaTempCali = reg.AppParam.AtpsRaTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsRaRhCali != app_set_reg.AppParam.AtpsRaRhCali) &&
				(reg.AppParam.AtpsRaRhCali >= -30) && 
				(reg.AppParam.AtpsRaRhCali <= 110) )
			{
				SysParam.AppParam.AtpsRaRhCali = reg.AppParam.AtpsRaRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsMaTempCali != app_set_reg.AppParam.AtpsMaTempCali) &&
				(reg.AppParam.AtpsMaTempCali >= -30) && 
				(reg.AppParam.AtpsMaTempCali <= 110) )
			{
				SysParam.AppParam.AtpsMaTempCali = reg.AppParam.AtpsMaTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsMaRhCali != app_set_reg.AppParam.AtpsMaRhCali) &&
				(reg.AppParam.AtpsMaRhCali >= -30) && 
				(reg.AppParam.AtpsMaRhCali <= 110) )
			{
				SysParam.AppParam.AtpsMaRhCali = reg.AppParam.AtpsMaRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsExaTempCali != app_set_reg.AppParam.AtpsExaTempCali) &&
				(reg.AppParam.AtpsExaTempCali >= -30) && 
				(reg.AppParam.AtpsExaTempCali <= 110) )
			{
				SysParam.AppParam.AtpsExaTempCali = reg.AppParam.AtpsExaTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsExaRhCali != app_set_reg.AppParam.AtpsExaRhCali) &&
				(reg.AppParam.AtpsExaRhCali >= -30) && 
				(reg.AppParam.AtpsExaRhCali <= 110) )
			{
				SysParam.AppParam.AtpsExaRhCali = reg.AppParam.AtpsExaRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsRoomaTempCali != app_set_reg.AppParam.AtpsRoomaTempCali) &&
				(reg.AppParam.AtpsRoomaTempCali >= -30) && 
				(reg.AppParam.AtpsRoomaTempCali <= 110) )
			{
				SysParam.AppParam.AtpsRoomaTempCali = reg.AppParam.AtpsRoomaTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsRoomaRhCali != app_set_reg.AppParam.AtpsRoomaRhCali) &&
				(reg.AppParam.AtpsRoomaRhCali >= -30) && 
				(reg.AppParam.AtpsRoomaRhCali <= 110) )
			{
				SysParam.AppParam.AtpsRoomaRhCali = reg.AppParam.AtpsRoomaRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsCoolaTempCali != app_set_reg.AppParam.AtpsCoolaTempCali) &&
				(reg.AppParam.AtpsCoolaTempCali >= -30) && 
				(reg.AppParam.AtpsCoolaTempCali <= 110) )
			{
				SysParam.AppParam.AtpsCoolaTempCali = reg.AppParam.AtpsCoolaTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsCoolaRhCali != app_set_reg.AppParam.AtpsCoolaRhCali) &&
				(reg.AppParam.AtpsCoolaRhCali >= -30) && 
				(reg.AppParam.AtpsCoolaRhCali <= 110) )
			{
				SysParam.AppParam.AtpsCoolaRhCali = reg.AppParam.AtpsCoolaRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsHeataTempCali != app_set_reg.AppParam.AtpsHeataTempCali) &&
				(reg.AppParam.AtpsHeataTempCali >= -30) && 
				(reg.AppParam.AtpsHeataTempCali <= 110) )
			{
				SysParam.AppParam.AtpsHeataTempCali = reg.AppParam.AtpsHeataTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsHeataRhCali != app_set_reg.AppParam.AtpsHeataRhCali) &&
				(reg.AppParam.AtpsHeataRhCali >= -30) && 
				(reg.AppParam.AtpsHeataRhCali <= 110) )
			{
				SysParam.AppParam.AtpsHeataRhCali = reg.AppParam.AtpsHeataRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsHumaTempCali != app_set_reg.AppParam.AtpsHumaTempCali) &&
				(reg.AppParam.AtpsHumaTempCali >= -30) && 
				(reg.AppParam.AtpsHumaTempCali <= 110) )
			{
				SysParam.AppParam.AtpsHumaTempCali = reg.AppParam.AtpsHumaTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsHumaRhCali != app_set_reg.AppParam.AtpsHumaRhCali) &&
				(reg.AppParam.AtpsHumaRhCali >= -30) && 
				(reg.AppParam.AtpsHumaRhCali <= 110) )
			{
				SysParam.AppParam.AtpsHumaRhCali = reg.AppParam.AtpsHumaRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsPreTempCali != app_set_reg.AppParam.AtpsPreTempCali) &&
				(reg.AppParam.AtpsPreTempCali >= -30) && 
				(reg.AppParam.AtpsPreTempCali <= 110) )
			{
				SysParam.AppParam.AtpsPreTempCali = reg.AppParam.AtpsPreTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsPreRhCali != app_set_reg.AppParam.AtpsPreRhCali) &&
				(reg.AppParam.AtpsPreRhCali >= -30) && 
				(reg.AppParam.AtpsPreRhCali <= 110) )
			{
				SysParam.AppParam.AtpsPreRhCali = reg.AppParam.AtpsPreRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsRotTempCali != app_set_reg.AppParam.AtpsRotTempCali) &&
				(reg.AppParam.AtpsRotTempCali >= -30) && 
				(reg.AppParam.AtpsRotTempCali <= 110) )
			{
				SysParam.AppParam.AtpsRotTempCali = reg.AppParam.AtpsRotTempCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AtpsRotRhCali != app_set_reg.AppParam.AtpsRotRhCali) &&
				(reg.AppParam.AtpsRotRhCali >= -30) && 
				(reg.AppParam.AtpsRotRhCali <= 110) )
			{
				SysParam.AppParam.AtpsRotRhCali = reg.AppParam.AtpsRotRhCali;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.Atmosphere != app_set_reg.AppParam.Atmosphere) &&
				(reg.AppParam.Atmosphere > 0)  &&
				(reg.AppParam.Atmosphere <= 200) )
			{
				SysParam.AppParam.Atmosphere = reg.AppParam.Atmosphere;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.SamplingPeriod != app_set_reg.AppParam.SamplingPeriod) &&
				(reg.AppParam.SamplingPeriod >= 100) && 
				(reg.AppParam.SamplingPeriod <= 5000) )
			{
				SysParam.AppParam.SamplingPeriod = reg.AppParam.SamplingPeriod;
				flags_to_set |= AppParamSaveFlag;
			}

			if( (reg.AppParam.AverageWindowSize != app_set_reg.AppParam.AverageWindowSize) &&
				(reg.AppParam.AverageWindowSize >= 3) && 
				(reg.AppParam.AverageWindowSize <= 50) )
			{
				SysParam.AppParam.AverageWindowSize = reg.AppParam.AverageWindowSize;
				flags_to_set |= AppParamSaveFlag;
			}
		}

		xEventGroupSetBits(SysEvent,  flags_to_set);
	}
}



/**
	* @brief AnlogInputCalibrations寄存器--读操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AnlogInputCaliReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint16_t *dat = (uint16_t *)modbus_buf;
	
	app_ai_cali_reg_fill( );
	
	iRegIndex = ( usAddress - AI_CALI_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_ai_cali_reg, sizeof(AppAnlogInputCalibrations) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}



/**
	* @brief AnlogInputCalibrations寄存器--写操作
  	* @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  	* @retval None
  */
static void AnlogInputCaliReg_Write( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
  	uint32_t iRegIndex;
	uint8_t temp1;
 	 uint32_t flags_to_set = 0;
	AppAnlogInputCalibrations reg;
	uint16_t *dat = (uint16_t *)modbus_buf; 
  
  	app_ai_cali_reg_fill( );
  
	iRegIndex = ( usAddress - AI_CALI_MODBUS_ADDRESS );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_ai_cali_reg, sizeof(AppAnlogInputCalibrations) );

	while( usNRegs > 0 )
	{
		dat[iRegIndex] = *RegBuffer++ << 8;
		dat[iRegIndex] |= *RegBuffer++;
		iRegIndex++;
		usNRegs--;
	}	
	
	memcpy( (uint8_t *)&reg, modbus_buf, sizeof(AppAnlogInputCalibrations) );
  
  
  temp1 = memcmp( (uint8_t *)&reg, (uint8_t *)&app_ai_cali_reg, sizeof(AppAnlogInputCalibrations) );
  if( temp1 != 0 )
  {
    
    
   
    
    
  }
}



/**
	* @brief AnlogOutputCalibrations寄存器--读操作
  	* @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  	* @retval None
  */
static void AnlogOutputCaliReg_Read( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint32_t iRegIndex;
	uint16_t *dat = (uint16_t *)modbus_buf;
	
	app_ao_cali_reg_fill( );
	
	iRegIndex = ( usAddress - AO_CALI_MODBUS_ADDRESS );

	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_ao_cali_reg, sizeof(AppAnlogOutputCalibrations) );
	
	while( usNRegs > 0 )
	{
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] >> 8 );
		*RegBuffer++ = ( uint8_t )( dat[iRegIndex] & 0xFF );
		iRegIndex++;
		usNRegs--;
	}
}



/**
	* @brief AnlogOutputCalibrations寄存器--写操作
  * @param RegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：	寄存器数量
  * @retval None
  */
static void AnlogOutputCaliReg_Write( uint8_t *RegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
  	uint32_t iRegIndex;
	uint8_t temp1;
  	uint32_t flags_to_set = 0;
	AppAnlogOutputCalibrations reg;
	uint16_t *dat = (uint16_t *)modbus_buf; 
  
  	app_ao_cali_reg_fill( );
  
	iRegIndex = ( usAddress - AO_CALI_MODBUS_ADDRESS );
	
	memset( modbus_buf, 0, sizeof(modbus_buf) );
	memcpy( modbus_buf, (uint8_t *)&app_ao_cali_reg, sizeof(AppAnlogOutputCalibrations) );

	while( usNRegs > 0 )
	{
		dat[iRegIndex] = *RegBuffer++ << 8;
		dat[iRegIndex] |= *RegBuffer++;
		iRegIndex++;
		usNRegs--;
	}	
	
	memcpy( (uint8_t *)&reg, modbus_buf, sizeof(AppAnlogOutputCalibrations) );
  
  
  temp1 = memcmp( (uint8_t *)&reg, (uint8_t *)&app_ao_cali_reg, sizeof(AppAnlogOutputCalibrations) );
  if( temp1 != 0 )
  {

    
    
    
    
  }
}




/**
  * @brief MODBUS--读输入寄存器回调函数
  * @param pucRegBuffer：存放读取的寄存器数据
  * @param usAddress：输入寄存器地址
  * @param usNRegs：读取寄存器的数量
  * @retval 
  */
int8_t eMBRegInputCB( uint8_t *pucRegBuffer, uint16_t usAddress, uint16_t usNRegs )
{
	uint8_t eStatus = 0;
	uint8_t status = 0;
	
	if( (usAddress >= DEBUG_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (DEBUG_MODBUS_ADDRESS + (sizeof(DebudROs)/2)) ) )
	{
		status = 0;
	}
	else 
	{
		return -AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
	}
	
	switch( status )
	{
		case 0:
		{
			DebugROReg_Read( pucRegBuffer, usAddress, usNRegs );
		}
		break;
	}

	return eStatus;
}




/**
  * @brief MODBUS--读写保持寄存器回调函数
  * @param pucRegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNRegs：寄存器数量
	* @param eMode：寄存器操作模式
  * @retval 
  */
int8_t eMBRegHoldingCB( uint8_t * pucRegBuffer, uint16_t usAddress, uint16_t usNRegs, uint8_t eMode )
{
	uint8_t eStatus = 0;
	uint8_t status = 0;
	
	if( (usAddress >= DEVICE_INFO_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (DEVICE_INFO_MODBUS_ADDRESS + (sizeof(AppDeviceInfos)/2)) ) )
	{
		status = 0;
	}
	else if( (usAddress >= APP_INPUT_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (APP_INPUT_MODBUS_ADDRESS + (sizeof(AppInputs)/2)) ) )
	{
		if( eMode != 0 ) return -AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION;
		status = 1;
	}
	else if( (usAddress >= CALIBRATION_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (CALIBRATION_MODBUS_ADDRESS + (sizeof(AppCalibrations)/2)) ) )
	{
		status = 2;
	}
	else if( (usAddress >= APP_ALARMS_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (APP_ALARMS_MODBUS_ADDRESS + (sizeof(AppAlarms)/2)) ) )
	{
		status = 3;
	}
	else if( (usAddress >= APP_STATUS_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (APP_STATUS_MODBUS_ADDRESS + (sizeof(AppStatus)/2)) ) )
	{
    if( eMode != 0 ) return -AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION;
		status = 4;
	}
	else if( (usAddress >= APP_SETTINGS_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (APP_SETTINGS_MODBUS_ADDRESS + (sizeof(AppSettings)/2)) ) )
	{
		status = 5;
	}
	else if( (usAddress >= APP_OUTPUT_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (APP_OUTPUT_MODBUS_ADDRESS + (sizeof(AppOutputs)/2)) ) )
	{
		status = 6;
	}
	else if( (usAddress >= APP_OVERRIDES_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (APP_OVERRIDES_MODBUS_ADDRESS + (sizeof(AppOverrides)/2)) ) )
	{
		status = 7;
	}
	else if( (usAddress >= DEBUG_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (DEBUG_MODBUS_ADDRESS + (sizeof(DebudRWs)/2)) ) )
	{
		status = 8;
	}
  else if( (usAddress >= AI_CALI_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (AI_CALI_MODBUS_ADDRESS + (sizeof(AppAnlogInputCalibrations)/2)) ) )
	{
		status = 9;
	}
  else if( (usAddress >= AO_CALI_MODBUS_ADDRESS) && ( (usAddress + usNRegs) <= (AO_CALI_MODBUS_ADDRESS + (sizeof(AppAnlogOutputCalibrations)/2)) ) )
	{
		status = 10;
	}
	else 
	{
		return -AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
	}
	
	switch ( eMode )
	{
		case 0:
		{
			if( status == 0 ) DeviceInfoReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 1 ) AppInputReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 2 ) CalibrationsReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 3 ) AppAlarmsReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 4 ) AppStatusReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 5 ) AppSetReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 6 ) AppOutputReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 7 ) AppOverridesReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 8 ) DebugRWReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 9 ) AnlogInputCaliReg_Read( pucRegBuffer, usAddress, usNRegs );
			else if( status == 10 ) AnlogOutputCaliReg_Read( pucRegBuffer, usAddress, usNRegs );
		}
		break;
		
		case 1:
		{
			if( status == 0 ) DeviceInfoReg_Write( pucRegBuffer, usAddress, usNRegs );
			else if( status == 2 ) CalibrationsReg_Write( pucRegBuffer, usAddress, usNRegs );
			else if( status == 3 ) AppAlarmsReg_Write( pucRegBuffer, usAddress, usNRegs );
			else if( status == 5 ) AppSetReg_Write( pucRegBuffer, usAddress, usNRegs );
			else if( status == 6 ) AppOutputReg_Write( pucRegBuffer, usAddress, usNRegs );
			else if( status == 7 ) AppOverridesReg_Write( pucRegBuffer, usAddress, usNRegs );
			else if( status == 8 ) DebugRWReg_Write( pucRegBuffer, usAddress, usNRegs );
			else if( status == 9 ) AnlogInputCaliReg_Write( pucRegBuffer, usAddress, usNRegs );
			else if( status == 10 ) AnlogOutputCaliReg_Write( pucRegBuffer, usAddress, usNRegs );
		}
		break;
	}
	
	return eStatus;
}




/**
  * @brief MODBUS--读写线圈寄存器回调函数
  * @param pucRegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNCoils：寄存器数量
	* @param eMode：寄存器操作模式
  * @retval 
  */
int8_t eMBRegCoilsCB( uint8_t *pucRegBuffer, uint16_t usAddress, uint16_t usNCoils, uint8_t eMode )
{
	uint8_t  eStatus = 0;
	uint16_t iRegIndex;
	uint16_t i = 0;
	uint8_t status;
	
	if( ( usAddress >= REG_COIL_START ) && ( (usAddress + usNCoils) <= (REG_COIL_START + REG_COIL_NREGS) ) )
	{
		iRegIndex = usAddress - usRegCoilStart;
		
		switch ( eMode )
		{
			 case 0:
			 {
				 for( i=0; i<usNCoils; i++ )
				 {
					 agile_modbus_slave_io_set( pucRegBuffer, i, usRegCoilBuf[iRegIndex+i] );
				 }
			 }
			 break;
			 
			 case 1:
			 {
				 for( i=0; i<usNCoils; i++ )
				 {
					 status = agile_modbus_slave_io_get( pucRegBuffer, i);
					 usRegCoilBuf[iRegIndex+i] = status;
				 }
			 }
			 break;
		 }
	}
	else
	{
		eStatus = -AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
	}

	return eStatus;
}





/**
  * @brief MODBUS--读离散输入寄存器回调函数
  * @param pucRegBuffer：存放寄存器数据
	* @param usAddress：寄存器地址
	* @param usNDiscrete：寄存器数量
  * @retval 
  */
int8_t eMBRegDiscreteCB( uint8_t *pucRegBuffer, uint16_t usAddress, uint16_t usNDiscrete )
{
	uint8_t  eStatus = 0;
	uint16_t iRegIndex;
	uint16_t i = 0;
	
	if( ( usAddress >= REG_DISC_START ) && ( (usAddress + usNDiscrete) <= (REG_DISC_START + REG_DISC_NREGS) ) )
	{
		iRegIndex = usAddress - usRegDiscStart;
		
		for( i=0; i<usNDiscrete; i++ )
		{
			agile_modbus_slave_io_set( pucRegBuffer, i, usRegDiscBuf[iRegIndex+i] );
		}
	}
	else
	{
		eStatus = -AGILE_MODBUS_EXCEPTION_ILLEGAL_DATA_ADDRESS;
	}

  return eStatus;
}


